import json
import subprocess

from component.AutoFactory import AutoFactory


def main():
    # 初始化工具工厂
    auto_factory = AutoFactory()
    # 测试打印数据库
    cursor = auto_factory.mysql.cursor
    cursor.execute("select * from  system_account ")
    data_account = cursor.fetchone()
    print(f"===============数据库查取用户数据：{data_account[2]}=================")
    # 操作工具
    opt = auto_factory.opt
    # 打开app
    login = True
    if opt.exists_text('行政版'):
        opt.click_text('行政版', 0, "点击应用图标")
        # 打开应用慢循环等待页面加载
        while login:
            opt.sleep_pt(1, "等待页面加载....")
            if opt.exists_text('请输入账号'):
                login = False
            if opt.exists_resource('com.wanggeyuan.zongzhi:id/fab01Add'):
                login = False

    opt.close_app()
    auto_factory.sys.exit(1)
    # ------------------------------未自动登录----------------------------#
    if opt.exists_text('请输入账号'):
        opt.obtain_element_text('请输入账号').set_text("13403636582")
        opt.sleep_pt(2)
    if opt.exists_text('请输入密码'):
        opt.obtain_element_text('请输入密码').set_text("bgfg1000lbfwlXP#")

    opt.sleep_pt(1, '等待登录')

    if opt.exists_resource('com.wanggeyuan.zongzhi:id/login_btn'):
        opt.click_resource('com.wanggeyuan.zongzhi:id/login_btn', 5, "点击登录")
        login_toast = opt.d.toast.get_message()
        # 获取toast,当没有找到toast消息时，返回default内容
        if login_toast == '用户名或密码错误':
            opt.pt("用户名或密码错误")
            opt.exit()
        # 清空toast缓存
        opt.d.toast.reset()

    # -----------------------------点击矛盾纠纷----------------------------#
    index = True
    opt.pt("加载首页")
    while index:
        opt.sleep_pt(1, "等待首页加载....")
        if opt.exists_resource('com.wanggeyuan.zongzhi:id/fab01Add'):
            index = False
    opt.click_resource('com.wanggeyuan.zongzhi:id/fab01Add', 2, "点击添加")
    opt.click_resource('com.wanggeyuan.zongzhi:id/fb_one', 2, "事件上报")
    # ---------------------------------------------结束-----------------------------------
    # 地理坐标填入
    #adb shell setprop persist.nox.gps.longitude 119.599162999999
    auto_factory.os.system("adb shell setprop persist.nox.gps.longitude 114.16")
    auto_factory.os.system("adb shell setprop persist.nox.gps.latitude  39.45")
    opt.sleep_pt(5)
    # ---------------------------------------------结束-----------------------------------
    opt.click_resource('com.wanggeyuan.zongzhi:id/shangbao_type', 2, "类型选择")
    # 抓取全部当前资源id下全部类型
    types = opt.obtain_element_resource('com.wanggeyuan.zongzhi:id/itemvalue')
    for type in types:
        if (type.info['text'] == '疫情防控'):
            opt.click_text('疫情防控', 2, "选择一类选项")
            typesSub = opt.obtain_element_resource('com.wanggeyuan.zongzhi:id/itemvalue')
            for e in typesSub:
                if (e.info['text'] == '疫情线索'):
                    opt.click_text('疫情线索', 2, "退出选择")
                    break
            break

    opt.click_resource('com.wanggeyuan.zongzhi:id/shangbao_chengduBtn', 1, "选择事件类型")
    opt.click_text('疑难复杂', 2, "点击类型小项")
    opt.click_resource('com.wanggeyuan.zongzhi:id/shijian_photo', 2, "点击相机")
    opt.click_text('相册', 2, "选择相册")
    photo_page = opt.obtain_element_resource('com.wanggeyuan.zongzhi:id/image_view_album_image')
    photo_page[0].click()
    opt.sleep_pt(2)
    photo_list = opt.obtain_element_resource('com.wanggeyuan.zongzhi:id/image_view_image_select')
    for e in photo_list:
        e.click()
        opt.sleep_pt(1)
    opt.click_class_name('android.support.v7.widget.LinearLayoutCompat', 2, "确认图像选择")
    # 定位
    # TODO
    # 事件描述：
    opt.obtain_element_resource('com.wanggeyuan.zongzhi:id/shangbao_miaoshu').set_text('hello world...')
    opt.sleep_pt(1, "取消|提交")
    opt.click_resource('com.wanggeyuan.zongzhi:id/img_fanhui', 2, "返回应用首页")
    opt.click_bounds(720, 2320, 1440, 2560, 2, "点击我的")
    opt.click_bounds(0, 1148, 1440, 1332, 2, "点击设置")
    opt.click_resource('com.wanggeyuan.zongzhi:id/logout_btn', 1, "退出")
    # 退出确认
    opt.click_resource('com.wanggeyuan.zongzhi:id/md_buttonDefaultPositive', 1, "确认退出")


if __name__ == "__main__":
    main()
